/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.garagesale.admin.action;

import com.garagesale.config.hibernate.HibernateUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.garagesale.data.DBAccess;
import com.garagesale.data.ItemCategory;
import com.garagesale.data.Item;
import com.garagesale.data.User;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;

/**
 *
 * @author stolworthyfamily
 */
public class AddItemCategoryAction extends org.apache.struts.action.Action
{

    /* forward name="success" path="" */
    private static final String SUCCESS = "welcome";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     *
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    String name = request.getParameter("name");
    String color = request.getParameter("labelColor");
    String image = request.getParameter("imageLocation");

    ItemCategory category = new ItemCategory(name, color, image);

    DBAccess.getInstance().create(category);

    return mapping.findForward(SUCCESS);
    }
    }
     **/
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {

        DBAccess dbAccess = DBAccess.getInstance();
        String add = request.getParameter("sub");
        String delete = request.getParameter("itemCategoryID");
        String name = request.getParameter("name");
        String color = request.getParameter("labelColor");
        String image = request.getParameter("imageLocation");

        if (delete != null && !delete.isEmpty())
        {
            try
            {
                Integer categoryId = Integer.parseInt(delete);
                Session session = HibernateUtil.getSessionFactory().openSession();
                ItemCategory cate = (ItemCategory) session.get(ItemCategory.class, categoryId);
                session.close();

                if (cate != null)
                {
                    dbAccess.delete(cate);
                }
            } catch (NumberFormatException nfe)
            {
            }
        }

        if (add != null && !add.isEmpty())
        {
            try
            {
                ItemCategory category = new ItemCategory(name, color, image);

                dbAccess.create(category);

            } catch (NumberFormatException nfe)
            {
            }
        }

        List<ItemCategory> categories = DBAccess.getInstance().getAllCategories();

        request.setAttribute("categories", categories);



        return mapping.findForward(SUCCESS);
    }
}
